package com.ikkong.platform.controller;

import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.platform.meta.intercept.MemberDocumentIntercept;
import com.ikkong.platform.model.MemberDocument;
import com.ikkong.platform.service.MemberDocumentService;
import com.ikkong.platform.service.impl.MemberDocumentServiceImpl;

/**
 * Generated by Blade.
 * 2018-07-18 10:28:55
 */
public class MemberDocumentController extends UrlPermissController {
	private static String CODE = "memberDocument";
	private static String PERFIX = "tb_member_document";
	private static String LIST_SOURCE = "MemberDocument.list";
	private static String BASE_PATH = "/platform/memberDocument/";
	
	MemberDocumentService service = new MemberDocumentServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "memberDocument.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "memberDocument_add.html");
	}

	public void edit() {
		String id = getPara(0);
		MemberDocument memberDocument = service.findById(id);
		setAttr("model", JsonKit.toJson(memberDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "memberDocument_edit.html");
	}

	public void view() {
		String id = getPara(0);
		MemberDocument memberDocument = service.findById(id);
		setAttr("model", JsonKit.toJson(memberDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "memberDocument_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new MemberDocumentIntercept());
		renderJson(grid);
	}

	public void save() {
		MemberDocument memberDocument = mapping(PERFIX, MemberDocument.class);
		int bank_id=memberDocument.getBank_id();
		int bussiness_id=memberDocument.getBussiness_id();
		int member_id=memberDocument.getMember_id();
		boolean flag=service.isExist("select * from tb_member_document where bank_id=#{bank_id}&&bussiness_id=#{bussiness_id}&&member_id=#{member_id}",
				Record.create().set("bank_id", bank_id)
								.set("bussiness_id", bussiness_id).set("member_id", member_id));
		if(flag){
			renderJson(success("该用户已存在当前模板"));
			return;
		}
		int crt_user=(int)ShiroKit.getUser().getId();
		memberDocument.setCrt_user(crt_user);
		boolean temp = service.save(memberDocument);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		MemberDocument memberDocument = mapping(PERFIX, MemberDocument.class);
		int crt_user=(int)ShiroKit.getUser().getId();
		memberDocument.setCrt_user(crt_user);
		boolean temp = service.update(memberDocument);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
